﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ChongJu.EventManager;

namespace ChoJuRating.View
{
    /// <summary>
    /// JYMessageBox.xaml 的交互逻辑
    /// </summary>
    public partial class CJMessageBox : Window
    {
        public bool Result { get; private set; }

        private bool _isBack;

        public CJMessageBox(string message, MsgTitle title = MsgTitle.Tip, bool viewCancel = false, bool isBack = false)
        {
            InitializeComponent();

            _isBack = isBack;
            if (title == MsgTitle.Tip)
            {
                tbkTitle.Text = "提示";
                header.Background = Brushes.LightSkyBlue;
            }
            else if (title == MsgTitle.Warning)
            {
                tbkTitle.Text = "警告";
                header.Background = Brushes.Orange;
            }
            else
            {
                tbkTitle.Text = "错误";
                header.Background = Brushes.Red;
            }

            tbkMesage.Text = MakeMessage(message);
            if (viewCancel == true)
                btnCancel.Visibility = Visibility.Visible;
            else
                btnCancel.Visibility = Visibility.Collapsed;

            btnCancel.Focus();

            if (message.Length < 50)
                this.Height = 460;
            else if (message.Length < 100)
                Height = 660;
            else
                this.Height = 750;

            this.Loaded += JYMessageBox_Loaded;
            this.Closed += JYMessageBox_Closed;
        }

        private void JYMessageBox_Closed(object sender, EventArgs e)
        {
            if (_isBack == false)
                CommonEvent.OnMianFromOpacity(false);
        }

        private void JYMessageBox_Loaded(object sender, RoutedEventArgs e)
        {
            CommonEvent.OnMianFromOpacity(true);
        }

        private void btnSure_Click(object sender, RoutedEventArgs e)
        {
            Result = true;
            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Result = false;
            this.Close();
        }

        private void close_Click(object sender, RoutedEventArgs e)
        {
            Result = false;
            this.Close();
        }

        private string MakeMessage(string msg)
        {
            StringBuilder sb = new StringBuilder();
            var row = msg.Length / 13 + ((msg.Length % 13) > 0 ? 1 : 0);

            for(int i = 0; i < row; i++)
            {
                if (i == row - 1)
                    sb.Append(msg.Substring(i * 13));
                else
                    sb.AppendLine(msg.Substring(i * 13, 13));
            }

            return sb.ToString();
        }
    }
}
